package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-hub/chone"
	"novel-hub/modler"
	"novel-hub/pkg"
	"strconv"
	"time"
)

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	redis_key := "UserLogin" + username
	errCount, _ := chone.Rdb.Get(chone.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user modler.User
	err := modler.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		chone.Rdb.Incr(chone.Ctx, redis_key)
		if errCount == 2 {
			chone.Rdb.Set(chone.Ctx, redis_key, "3", time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
}

func UserAdd(c *gin.Context) {
	title := c.PostForm("title")
	author := c.PostForm("author")
	comment := c.PostForm("comment")
	Type := c.PostForm("type")
	if title == "" || author == "" || comment == "" || Type == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	video := modler.Video{

		Title:   title,
		Author:  author,
		Comment: comment,
		Type:    Type,
	}
	err := modler.DB.Create(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func UserList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var count int64
	modler.DB.Model(&modler.Video{}).Count(&count)
	var list []modler.Video

	redis_key := "UserList" + strconv.Itoa(page)
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Offset(offset).Limit(size).Debug().Find(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}

func UserDetail(c *gin.Context) {
	id := c.Query("id")
	var list modler.Video

	redis_key := "UserDetail" + id
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Where("id=?", id).First(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func Del(c *gin.Context) {
	id := c.Query("id")
	var video modler.Video
	modler.DB.Where("id=?", id).Delete(&video)
	redis_key := "Del" + id
	chone.Rdb.Get(chone.Ctx, redis_key)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
